﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Things.IDAL;
using Things.DBUtility;//Please add references
namespace Things.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:BuyerAddress
	/// </summary>
	public partial class BuyerAddress:IBuyerAddress
	{
		public BuyerAddress()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid BuyerAddressID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Things_BuyerAddress");
			strSql.Append(" where BuyerAddressID=@BuyerAddressID ");
			SqlParameter[] parameters = {
					new SqlParameter("@BuyerAddressID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = BuyerAddressID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Things.Model.BuyerAddress model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Things_BuyerAddress(");
			strSql.Append("BuyerAddressID,BuyerAccount,BuyerAddressName,IsDefault,ZipCode,BuyerAddressCity,ReceiverName,ReceiverMobile,ReceiverTelephone)");
			strSql.Append(" values (");
			strSql.Append("@BuyerAddressID,@BuyerAccount,@BuyerAddressName,@IsDefault,@ZipCode,@BuyerAddressCity,@ReceiverName,@ReceiverMobile,@ReceiverTelephone)");
			SqlParameter[] parameters = {
					new SqlParameter("@BuyerAddressID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@BuyerAccount", SqlDbType.NVarChar,20),
					new SqlParameter("@BuyerAddressName", SqlDbType.NVarChar,100),
					new SqlParameter("@IsDefault", SqlDbType.Bit,1),
					new SqlParameter("@ZipCode", SqlDbType.NVarChar,10),
					new SqlParameter("@BuyerAddressCity", SqlDbType.NVarChar,100),
					new SqlParameter("@ReceiverName", SqlDbType.NVarChar,100),
					new SqlParameter("@ReceiverMobile", SqlDbType.NVarChar,50),
					new SqlParameter("@ReceiverTelephone", SqlDbType.NVarChar,50)};
			parameters[0].Value = Guid.NewGuid();
			parameters[1].Value = model.BuyerAccount;
			parameters[2].Value = model.BuyerAddressName;
			parameters[3].Value = model.IsDefault;
			parameters[4].Value = model.ZipCode;
			parameters[5].Value = model.BuyerAddressCity;
			parameters[6].Value = model.ReceiverName;
			parameters[7].Value = model.ReceiverMobile;
			parameters[8].Value = model.ReceiverTelephone;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Things.Model.BuyerAddress model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Things_BuyerAddress set ");
			strSql.Append("BuyerAccount=@BuyerAccount,");
			strSql.Append("BuyerAddressName=@BuyerAddressName,");
			strSql.Append("IsDefault=@IsDefault,");
			strSql.Append("ZipCode=@ZipCode,");
			strSql.Append("BuyerAddressCity=@BuyerAddressCity,");
			strSql.Append("ReceiverName=@ReceiverName,");
			strSql.Append("ReceiverMobile=@ReceiverMobile,");
			strSql.Append("ReceiverTelephone=@ReceiverTelephone");
			strSql.Append(" where BuyerAddressID=@BuyerAddressID ");
			SqlParameter[] parameters = {
					new SqlParameter("@BuyerAccount", SqlDbType.NVarChar,20),
					new SqlParameter("@BuyerAddressName", SqlDbType.NVarChar,100),
					new SqlParameter("@IsDefault", SqlDbType.Bit,1),
					new SqlParameter("@ZipCode", SqlDbType.NVarChar,10),
					new SqlParameter("@BuyerAddressCity", SqlDbType.NVarChar,100),
					new SqlParameter("@ReceiverName", SqlDbType.NVarChar,100),
					new SqlParameter("@ReceiverMobile", SqlDbType.NVarChar,50),
					new SqlParameter("@ReceiverTelephone", SqlDbType.NVarChar,50),
					new SqlParameter("@BuyerAddressID", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = model.BuyerAccount;
			parameters[1].Value = model.BuyerAddressName;
			parameters[2].Value = model.IsDefault;
			parameters[3].Value = model.ZipCode;
			parameters[4].Value = model.BuyerAddressCity;
			parameters[5].Value = model.ReceiverName;
			parameters[6].Value = model.ReceiverMobile;
			parameters[7].Value = model.ReceiverTelephone;
			parameters[8].Value = model.BuyerAddressID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 更新所有记录的默认地址为False
        /// </summary>
        public bool UpdateDefaultAddress()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Things_BuyerAddress set ");
            strSql.Append("IsDefault=0");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid BuyerAddressID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Things_BuyerAddress ");
			strSql.Append(" where BuyerAddressID=@BuyerAddressID ");
			SqlParameter[] parameters = {
					new SqlParameter("@BuyerAddressID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = BuyerAddressID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string BuyerAddressIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Things_BuyerAddress ");
			strSql.Append(" where BuyerAddressID in ("+BuyerAddressIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Things.Model.BuyerAddress GetModel(Guid BuyerAddressID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 BuyerAddressID,BuyerAccount,BuyerAddressName,IsDefault,ZipCode,BuyerAddressCity,ReceiverName,ReceiverMobile,ReceiverTelephone from Things_BuyerAddress ");
			strSql.Append(" where BuyerAddressID=@BuyerAddressID ");
			SqlParameter[] parameters = {
					new SqlParameter("@BuyerAddressID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = BuyerAddressID;

			Things.Model.BuyerAddress model=new Things.Model.BuyerAddress();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["BuyerAddressID"]!=null && ds.Tables[0].Rows[0]["BuyerAddressID"].ToString()!="")
				{
					model.BuyerAddressID= new Guid(ds.Tables[0].Rows[0]["BuyerAddressID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["BuyerAccount"]!=null && ds.Tables[0].Rows[0]["BuyerAccount"].ToString()!="")
				{
					model.BuyerAccount=ds.Tables[0].Rows[0]["BuyerAccount"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BuyerAddressName"]!=null && ds.Tables[0].Rows[0]["BuyerAddressName"].ToString()!="")
				{
					model.BuyerAddressName=ds.Tables[0].Rows[0]["BuyerAddressName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["IsDefault"]!=null && ds.Tables[0].Rows[0]["IsDefault"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsDefault"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsDefault"].ToString().ToLower()=="true"))
					{
						model.IsDefault=true;
					}
					else
					{
						model.IsDefault=false;
					}
				}
				if(ds.Tables[0].Rows[0]["ZipCode"]!=null && ds.Tables[0].Rows[0]["ZipCode"].ToString()!="")
				{
					model.ZipCode=ds.Tables[0].Rows[0]["ZipCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BuyerAddressCity"]!=null && ds.Tables[0].Rows[0]["BuyerAddressCity"].ToString()!="")
				{
					model.BuyerAddressCity=ds.Tables[0].Rows[0]["BuyerAddressCity"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ReceiverName"]!=null && ds.Tables[0].Rows[0]["ReceiverName"].ToString()!="")
				{
					model.ReceiverName=ds.Tables[0].Rows[0]["ReceiverName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ReceiverMobile"]!=null && ds.Tables[0].Rows[0]["ReceiverMobile"].ToString()!="")
				{
					model.ReceiverMobile=ds.Tables[0].Rows[0]["ReceiverMobile"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ReceiverTelephone"]!=null && ds.Tables[0].Rows[0]["ReceiverTelephone"].ToString()!="")
				{
					model.ReceiverTelephone=ds.Tables[0].Rows[0]["ReceiverTelephone"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select BuyerAddressID,BuyerAccount,BuyerAddressName,IsDefault,ZipCode,BuyerAddressCity,ReceiverName,ReceiverMobile,ReceiverTelephone ");
			strSql.Append(" FROM Things_BuyerAddress ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" BuyerAddressID,BuyerAccount,BuyerAddressName,IsDefault,ZipCode,BuyerAddressCity,ReceiverName,ReceiverMobile,ReceiverTelephone ");
			strSql.Append(" FROM Things_BuyerAddress ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Things_BuyerAddress ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.BuyerAddressID desc");
			}
			strSql.Append(")AS Row, T.*  from Things_BuyerAddress T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Things_BuyerAddress";
			parameters[1].Value = "BuyerAddressID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

